package com.qingxin.park.service;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.MaterialProduct;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 物资
 * @author Administrator
 *
 */
@Service
public class MaterialProductService extends BaseService<MaterialProduct, String> {
	
	/**
	 * 列表数据
	 * @param materialid
	 * @param search
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	
	public Pager list(String leftdata,String search, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from MaterialProduct");
		if(StringUtils.isNotEmpty(leftdata)){
			querier.append(" where manufacturerid=:manufacturerid");
			querier.setParam("manufacturerid", leftdata);
		}
		if(StringUtils.isNotEmpty(search)){
			search = search.replace("'", "");
			querier.append(" and (materialName like :materialName or materialCode like :materialCode) ");
			querier.setParam("materialName", "%"+search+"%");
			querier.setParam("materialCode", "%"+search+"%");
		}
		querier.append(pageParam.getOrderby(" order by materialName asc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 添加信息
	 * @param materialProduct
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(MaterialProduct materialProduct,LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		materialProduct.setCreateTime(now);
		materialProduct.setUpdateTime(now);
		materialProduct.setCreateBy(loginUser.getLogin_name());
		materialProduct.setUpdateBy(loginUser.getLogin_name());
		
		if(StringUtils.isNotEmpty(materialProduct.getMaterialImg())){
			UploadFile.moveTmpTo(materialProduct.getMaterialImg(), UploadFile.DIR_MATERIAL_IMG);
		}
		
		this.save(materialProduct);
		return null;
	}
	
	/**
	 * 更新信息
	 * @param materialProduct
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(MaterialProduct materialProduct, LoginUser loginUser, HttpServletRequest request){
		MaterialProduct old = getSafeEntity(materialProduct.getMaterialid());
		
		if(StringUtils.isNotEmpty(materialProduct.getMaterialImg()) && !materialProduct.getMaterialImg().equals(old.getMaterialImg())){
			UploadFile.moveTmpTo(materialProduct.getMaterialImg(), UploadFile.DIR_MATERIAL_IMG);
		}
		
		String fields = "materialCode,materialName,pbn,units,remark,materialImg";
		BeanUtil.setTargetFields(old, materialProduct, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		Updater<MaterialProduct> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
		
	}
	
	
	/**
	 * 删除信息
	 * @param materialids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] materialids, LoginUser loginUser, HttpServletRequest request){
		for(String materialid : materialids){
			MaterialProduct material = get(materialid);
			if(material==null){
				continue;
			}
			this.delete(material);
		}
		
		return null;
	}
}
